﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.WebUI.Infrastructure.Concrete;
//using System.Drawing;
//using System.Drawing.Imaging;
//using System.IO;
using Zatify.Domain.Helpers;

namespace Zatify.WebUI.Infrastructure
{
    public class BaseController : Controller
    {
        protected virtual new ZatifyPrincipal User
        {
            get { return HttpContext.User as ZatifyPrincipal; }
        }

        protected byte[] UploadedTempImageBytes
        {
            get { return (byte[])Session["UploadedImageBytes"]; }
            set { Session["UploadedImageBytes"] = value; }
        }
        protected string UploadedTempImageMimeType
        {
            get { return (string)Session["UploadedImageMimeType"]; }
            set { Session["UploadedImageMimeType"] = value; }
        }

        public ActionResult UploadTempImage(HttpPostedFileBase datafile)
        {
            var tempFolderPath = @"~\Content\uploads\temp\";

            var flPath = datafile.FileName;

            if (flPath.Contains("\\"))
                flPath = flPath.Remove(0, (flPath.LastIndexOf("\\") + 1));

            var savePath = Server.MapPath(tempFolderPath + flPath);
            datafile.SaveAs(savePath);

            return Content(Url.Content(tempFolderPath + flPath));
        }

        public ActionResult UploadTempImageAndTumb(HttpPostedFileBase datafile, string userName)
        {
            this.UploadedTempImageBytes = new byte[datafile.InputStream.Length];
            this.UploadedTempImageMimeType = datafile.ContentType;

            datafile.InputStream.Read(this.UploadedTempImageBytes, 0, (int)datafile.InputStream.Length); /* Creates Image from specified data stream */
            datafile.InputStream.Flush();

            return Content(String.Format("GetUploadedImage?img={0}", Guid.NewGuid().ToString()));
        }

        public FileContentResult GetUploadedImage(string img)
        {
            using (var thumb = Zatify.Domain.Helpers.Helper.ScaleImage(this.UploadedTempImageBytes, 150, 150))
            {
                return File(Helper.ImageToByteArray(thumb), this.UploadedTempImageMimeType);
            }
        }

        //public ActionResult UploadTempImageAndTumb(HttpPostedFileBase datafile, string userName)
        //{
        //    //System.Threading.Thread.Sleep(3000);
        //    using (var image = Image.FromStream(datafile.InputStream, true, true)) /* Creates Image from specified data stream */
        //    {
        //        //using (var thumb = image.GetThumbnailImage(151, 151, () => false, IntPtr.Zero))
        //        using (var thumb = Zatify.Domain.Helpers.Helper.ScaleImage(image, 150, 150))
        //        {
        //            /* Returns array of image encoder objects built into GDI+ */
        //            var jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == datafile.ContentType).First();

        //            using (var encParams = new EncoderParameters(1))
        //            {
        //                var fileName = datafile.FileName;
        //                if (fileName.Contains("\\"))
        //                    fileName = fileName.Remove(0, (fileName.LastIndexOf("\\") + 1));

        //                var tempFolder = "~/Content/uploads/temp/promotions/thumbnails/";
        //                var tempFileName = userName + "_" + fileName;
        //                var appDataThumbnailPath = Server.MapPath(tempFolder);

        //                if (!Directory.Exists(appDataThumbnailPath))
        //                {
        //                    Directory.CreateDirectory(appDataThumbnailPath);
        //                }

        //                var outputPath = Path.Combine(appDataThumbnailPath, tempFileName);
        //                long quality = 100;

        //                encParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
        //                thumb.Save(outputPath, jpgInfo, encParams);

        //                return Content(Url.Content(tempFolder + tempFileName));
        //            }
        //        }
        //    }
        //}
    }
}